#include "mainwindow.h"
const QString srcpath = ":/new/prefix1/";
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //
    mdiArea = new QMdiArea;
    mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);//设置滚动条
    mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);

    setCentralWidget(mdiArea);//将子窗口设置为中心窗口部件

    //connect(mdiArea,QMdiArea::subWindowActivated(QMdiSubWindow*),this,MainWindow::updateMenus());
    connect(mdiArea,&QMdiArea::subWindowActivated,this,&MainWindow::updateMenus);
    windowMapper = new QSignalMapper(this);
    //connect(windowMapper,QSignalMapper::mapping(QWidget *),this,MainWindow::setActiveSubWindow(QWidget *));
    //connect(windowMapper,&QSignalMapper::mapped,this,&MainWindow::setActiveSubWindow);
    //connect(windowMapper,SIGNAL(mappedObject(QObject*)),this,SLOT(setActiveSubWindow(QWidget *)));
    connect(windowMapper,&QSignalMapper::mappedObject,this,
            [=](QObject * object){
                qDebug()<<"connect(windowMapper,&QSignalMapper::mappedObject";
                QWidget  * widegt = qobject_cast<QWidget *>(object);
                setActiveSubWindow(widegt);
            }
            );//将点击的子窗口设置为活动子窗口

    creatAction();
    creatMenus();
    creatToolBars();
    creatStatusBar();
    updateMenus();

    resize(800,600);
    setWindowTitle("简易word 0.1");
    //
    setUnifiedTitleAndToolBarOnMac(true);
}

MainWindow::~MainWindow() {}

void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
    QTextCursor cursor = activeChils()->textCursor();
    if (!cursor.hasSelection())
        cursor.select(QTextCursor::WordUnderCursor);
    cursor.mergeCharFormat(format);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    mdiArea->closeAllSubWindows();
    if(mdiArea->currentSubWindow())
    {
        event->ignore();
    }
    else
    {
        event->accept();
    }
}

void MainWindow::fileNew()             //新建
{
    child *mychild = creatchild();
    mychild->newFile();
    mychild->show();
    enabledText();
}
void MainWindow::fileOpen()            //打开
{
    qDebug()<<"MainWindow::fileOpen()";
    QString filename = QFileDialog::getOpenFileName(this,tr("打开"),QString(),tr("HTML 文档(*.htm *.html);;所有文件(*.*)"));
    if(!filename.isEmpty())
    {
        qDebug()<<"if(!filename.isEmpty())";
        QMdiSubWindow *exiting = findChild(filename);
        if(exiting)
        {
            mdiArea->setActiveSubWindow(exiting);
            qDebug()<<"if(exiting)返回值为真";
            return;
        }
        else
        {
            qDebug()<<"if(exiting)返回值为假";
        }
        child * mychild = creatchild();
        if(mychild->loadFile(filename))
        {
            qDebug()<<"mychild->loadFile返回值为真";
            statusBar()->showMessage(tr("文件已经加载"),2000);
            mychild->show();
            enabledText();
        }
        else
        {
            qDebug()<<"mychild->loadFile返回值为假";
            mychild->close();
        }
    }
}
void MainWindow::fileSave()            //保存
{
    if(activeChils() &&activeChils()->save())
        statusBar()->showMessage(tr( "word文档保存成功."),2000);

}
void MainWindow::fileSaveAs()          //另存为
{
    if(activeChils() &&activeChils()->saveAs())
        statusBar()->showMessage(tr( "word文档另存为成功."));
}
void MainWindow::fileprint()           //打印
{
    QPrinter printer(QPrinter::HighResolution);
    QPrintDialog *pdlg = new QPrintDialog(&printer,this);
    if(activeChils()->textCursor().hasSelection())
    {
        pdlg->setOption(QAbstractPrintDialog::PrintSelection);
    }
    pdlg->setWhatsThis(tr("打印文档"));
    if(QDialog::Accepted == pdlg->exec())
    {
        activeChils()->print(&printer);
    }
    delete pdlg;
}
void MainWindow::filePrintPreview()    //文件打开预览
{
    QPrinter printer;//(QPrinter::HighResolution)
    QPrintPreviewDialog *preview = new QPrintPreviewDialog(&printer,this);
    connect(preview,&QPrintPreviewDialog::paintRequested,this,&MainWindow::printPreview);
    delete preview;
}
void MainWindow::printPreview(QPrinter *qprinter)        //打印预览
{
    activeChils()->print(qprinter);
}

void MainWindow::undo()    //撤销
{
    if(activeChils())
        activeChils()->undo();
}
void MainWindow::redo()    //重做
{
    if(activeChils())
        activeChils()->redo();
}
void MainWindow::cut()     //剪切
{
    if(activeChils())
        activeChils()->cut();
}
void MainWindow::copy()    //复制
{
    if(activeChils())
        activeChils()->copy();
}
void MainWindow::paste()   //粘贴
{
    if(activeChils())
        activeChils()->paste();
}
void MainWindow::about()   //关于
{
    QMessageBox::about(this,tr("关于"),tr("基于QT"));
}

void MainWindow::aboutQt()
{

}

void MainWindow::textBold()//
{
    QTextCharFormat fmt;
    fmt.setFontWeight(boldAct->isChecked() ? QFont::Bold : QFont::Normal);
    if(activeChils())
        activeChils()->mergeFormationOnWordOrSelection(fmt);
}
void MainWindow::textItalic()//
{
    QTextCharFormat fmt;
    fmt.setFontItalic(italicAct->isChecked());
    if(activeChils())
        activeChils()->mergeFormationOnWordOrSelection(fmt);
}
void MainWindow::textUnderline()//
{
    QTextCharFormat fmt;
    fmt.setFontUnderline(underlineAct->isChecked());
    if(activeChils())
        activeChils()->mergeFormationOnWordOrSelection(fmt);
}
void MainWindow::textAlign(QAction *a)//
{
    if(activeChils())
    {
        if(leftAlignAct == a)
        {
            activeChils()->setAlign(1);
        }
        else if(centerAct == a)
        {
            activeChils()->setAlign(2);
        }
        else if(rightAlignAct == a)
        {
            activeChils()->setAlign(3);
        }
        else if(justifyAct == a)
        {
            activeChils()->setAlign(4);
        }
    }
}
void MainWindow::textStyle(int styleIndex)//
{
    if(activeChils())
    {
        activeChils()->setStyle(styleIndex);
    }
}
void MainWindow::textFamily(const QFont &f)//
{
    //接字体，设置传递给child类的函数进行设置
    QTextCharFormat fmt;
    fmt.setFont(f);
    if(activeChils())
    {
        activeChils()->mergeFormationOnWordOrSelection(fmt);
    }
}
void MainWindow::textSize(const int &p)//
{
    QList<int> list;
    list<<6<<7<<8<<9<<10<<11<<12<<14<<16<<18<<20<<22<<24<<26<<28<<36<<48<<72;
    int pointsize = list.at(p);
    //qDebug() <<pointsize;
    if(p >= 0)
    {
        QTextCharFormat fmt;
        fmt.setFontPointSize(pointsize);
        if(activeChils())
        {
            qDebug() << "Merging format...";
            activeChils()->mergeFormationOnWordOrSelection(fmt);
        }
        //comboSize->setCurrentIndex(comboSize->currentIndex());
    }
}
void MainWindow::textColor()//
{
    if(activeChils())
    {
        QColor color = QColorDialog::getColor(activeChils()->textColor(),this);
        if(!color.isValid())
        {
            return;
        }
        QTextCharFormat fmt;
        fmt.setForeground(color);
        activeChils()->mergeFormationOnWordOrSelection(fmt);
        colorChanged(color);
    }
}

void MainWindow::updateMenus()//更新菜单
{
    bool haschild = (activeChils() != 0);

    saveAct->setEnabled(haschild);
    saveAsAct->setEnabled(haschild);
    printAct->setEnabled(haschild);
    printPreviewAct->setEnabled(haschild);
    pasteAct->setEnabled(haschild);

    closeAct->setEnabled(haschild);
    closeAllAct->setEnabled(haschild);
    tileAct->setEnabled(haschild);
    cascadeAct->setEnabled(haschild);
    nextAct->setEnabled(haschild);
    previousAct->setEnabled(haschild);
    separatorAct->setEnabled(haschild);

    bool hasSelection = (activeChils() && activeChils()->textCursor().hasSelection());
    cutAct->setEnabled(hasSelection);
    copyAct->setEnabled(hasSelection);
    boldAct->setEnabled(hasSelection);
    italicAct->setEnabled(hasSelection);
    underlineAct->setEnabled(hasSelection);
    leftAlignAct->setEnabled(hasSelection);
    centerAct->setEnabled(hasSelection);
    rightAlignAct->setEnabled(hasSelection);
    justifyAct->setEnabled(hasSelection);
    colorAct->setEnabled(hasSelection);
}
void MainWindow::updateWindowMenu()//
{
    windowMenu->clear();
    windowMenu->addAction(closeAct);
    windowMenu->addAction(closeAllAct);
    windowMenu->addSeparator();
    windowMenu->addAction(tileAct);
    windowMenu->addAction(cascadeAct);
    windowMenu->addSeparator();
    windowMenu->addAction(nextAct);
    windowMenu->addAction(previousAct);
    windowMenu->addAction(separatorAct);

    QList<QMdiSubWindow *> windows = mdiArea->subWindowList();
    separatorAct->setVisible(!windows.isEmpty());

    for(int i = 1;i<windows.size();i++)
    {
        child *mychild = qobject_cast<child *>(windows.at(i)->widget());
        QString text;
        if(i<9)
        {
            text = tr("&%1 %2").arg(i+1).arg(mychild->userFriendlyCurrentfile());
        }
        else
        {
            text = tr("%1 %2").arg(i+1).arg(mychild->userFriendlyCurrentfile());
        }

        QAction *action = windowMenu->addAction(text);
        action->setCheckable(true);
        action->setChecked(mychild == activeChils());

        connect(action, SIGNAL(triggered()), windowMapper, SLOT(map()));
        windowMapper->setMapping(action,windows.at(i));
    }

    enabledText();
}
void MainWindow::setActiveSubWindow(QWidget *window)//设置激活子窗口
{
    if(!window)
        return;
    mdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow*>(window));
}

void MainWindow::creatAction()     //创建菜单操作
{
    //
    newAct = new QAction(QIcon(":/new/prefix1/filenew.png"),tr("新建(&N)"),this);
    newAct->setShortcuts(QKeySequence::New);
    newAct->setToolTip("新建");//设置工具栏按钮提示信息
    newAct->setStatusTip(tr("创建一个新的word文档."));//设置状态栏提示文本信息
    connect(newAct,&QAction::triggered,this,&MainWindow::fileNew);

    openAct = new QAction(QIcon(srcpath + "file-open.png"),tr("打开(&O)"),this);
    openAct->setShortcuts(QKeySequence::Open);
    openAct->setToolTip("打开");//设置工具栏按钮提示信息
    openAct->setStatusTip(tr("打开word文档."));//设置状态栏提示文本信息
    connect(openAct,&QAction::triggered,this,&MainWindow::fileOpen);

    saveAct = new QAction(QIcon(srcpath + "filesave.png"),tr("保存(&S)"),this);
    saveAct->setShortcuts(QKeySequence::New);
    saveAct->setToolTip("保存");//设置工具栏按钮提示信息
    saveAct->setStatusTip(tr("保存当前word文档."));//设置状态栏提示文本信息
    connect(saveAct,&QAction::triggered,this,&MainWindow::fileSave);

    saveAsAct = new QAction(tr("另存为(&N)"),this);
    saveAsAct->setShortcuts(QKeySequence::SaveAs);
    //saveAsAct->setToolTip("新建");//设置工具栏按钮提示信息
    saveAsAct->setStatusTip(tr("另存为一个新的word文档."));//设置状态栏提示文本信息
    connect(saveAsAct,&QAction::triggered,this,&MainWindow::fileSaveAs);

    exitAct = new QAction(tr("退出(X)"),this);
    exitAct->setShortcuts(QKeySequence::Quit);
    exitAct->setStatusTip("退出");
    connect(exitAct,&QAction::triggered,qApp,&QApplication::closeAllWindows);

    printAct = new QAction(QIcon(srcpath + "filePrint.png"),tr("打印(&P)"),this);
    printAct->setShortcuts(QKeySequence::Print);
    printAct->setToolTip("打印");//设置工具栏按钮提示信息
    printAct->setStatusTip(tr("打印当前word文档."));//设置状态栏提示文本信息
    connect(printAct,&QAction::triggered,this,&MainWindow::fileprint);

    printPreviewAct = new QAction(tr("打印预览..."),this);
    printPreviewAct->setStatusTip(tr("打印预览当前word文档."));
    connect(printPreviewAct,&QAction::triggered,this,&MainWindow::filePrintPreview);

    undoAct = new QAction(QIcon(srcpath + "editundo.png"),tr("撤销(&U)"),this);
    undoAct->setShortcuts(QKeySequence::Undo);
    undoAct->setToolTip("撤销");
    undoAct->setStatusTip(tr("撤销当前word文档操作."));
    connect(undoAct,&QAction::triggered,this,&MainWindow::undo);

    redoAct = new QAction(QIcon(srcpath + "editredo.png"),tr("重做(&R)"),this);
    redoAct->setShortcuts(QKeySequence::Redo);
    redoAct->setToolTip("重做");
    redoAct->setStatusTip(tr("恢复之前word文档操作."));
    connect(redoAct,&QAction::triggered,this,&MainWindow::redo);

    cutAct = new QAction(QIcon(srcpath + "editcut.png"),tr("剪切(&T)"),this);
    cutAct->setShortcuts(QKeySequence::Cut);
    cutAct->setToolTip("剪切");
    cutAct->setStatusTip(tr("剪切所选内容放到剪贴板"));
    connect(cutAct,&QAction::triggered,this,&MainWindow::cut);

    copyAct = new QAction(QIcon(srcpath + "editcopy.png"),tr("复制(&C)"),this);
    copyAct->setShortcuts(QKeySequence::Copy);
    copyAct->setToolTip("复制");
    copyAct->setStatusTip(tr("复制所选内容到剪切板"));
    connect(copyAct,&QAction::triggered,this,&MainWindow::copy);

    pasteAct = new QAction(QIcon(srcpath + "editpaste.png"),tr("粘贴(&P)"),this);
    pasteAct->setShortcuts(QKeySequence::Paste);
    pasteAct->setToolTip("粘贴");
    pasteAct->setStatusTip(tr("将剪切板内容粘贴到"));
    connect(pasteAct,&QAction::triggered,this,&MainWindow::paste);

    //加粗
    boldAct = new QAction(QIcon(srcpath + "textbold.png"),tr("加粗(&B)"),this);
    boldAct->setCheckable(true);
    boldAct->setShortcuts(QKeySequence::Bold);
    boldAct->setToolTip("加粗");
    boldAct->setStatusTip(tr("将选中文字加粗"));
    QFont bold;
    bold.setBold(true);
    boldAct->setFont(bold);
    connect(boldAct,&QAction::triggered,this,&MainWindow::textBold);

    //倾斜
    italicAct = new QAction(QIcon(srcpath + "TextItalic.png"),tr("倾斜(&I)"),this);
    italicAct->setCheckable(true);
    italicAct->setShortcuts(QKeySequence::Italic);
    italicAct->setToolTip("倾斜");
    italicAct->setStatusTip(tr("将选中文字倾斜"));
    QFont italic;
    italic.setBold(true);
    italicAct->setFont(italic);
    connect(italicAct,&QAction::triggered,this,&MainWindow::textItalic);

    //下划线
    underlineAct = new QAction(QIcon(srcpath + "textunder.png"),tr("下划线(&U)"),this);
    underlineAct->setCheckable(true);
    underlineAct->setShortcuts(QKeySequence::Underline);
    underlineAct->setToolTip("下划线");
    underlineAct->setStatusTip(tr("为选中文字添加下划线"));
    QFont underline;
    underline.setBold(true);
    underlineAct->setFont(underline);
    connect(underlineAct,&QAction::triggered,this,&MainWindow::textUnderline);

    //格式，段落
    QActionGroup *grp = new QActionGroup(this);
    connect(grp,&QActionGroup::triggered,this,&MainWindow::textAlign);

    if(QApplication::isLeftToRight())
    {
        leftAlignAct = new QAction(QIcon(srcpath + "textleft.png"),tr("左对齐(&L)"),grp);
        centerAct = new QAction(QIcon(srcpath + "textcenter.png"),tr("居中对齐(&E)"),grp);
        rightAlignAct = new QAction(QIcon(srcpath + "textright.png"),tr("右对齐(&R)"),grp);
    }
    else
    {
        rightAlignAct = new QAction(QIcon(srcpath + "textright.png"),tr("右对齐(&R)"),grp);
        centerAct = new QAction(QIcon(srcpath + "textcenter.png"),tr("居中对齐(&E)"),grp);
        leftAlignAct = new QAction(QIcon(srcpath + "textleft.png"),tr("左对齐(&L)"),grp);
    }
    justifyAct = new QAction(QIcon(srcpath + "textjustify.png"),tr("两端对齐(&J)"),grp);

    //注意设置快捷键的函数，带s的不能用下面这种
    leftAlignAct->setShortcut(Qt::CTRL + Qt::Key_L);
    leftAlignAct->setCheckable(true);
    leftAlignAct->setToolTip("左对齐");
    leftAlignAct->setStatusTip(tr("将选择的文字进行左对齐"));

    centerAct->setShortcut(Qt::CTRL + Qt::Key_E);
    centerAct->setCheckable(true);
    centerAct->setToolTip("居中对齐");
    centerAct->setStatusTip(tr("将选择的文字进行居中对齐"));

    rightAlignAct->setShortcut(Qt::CTRL + Qt::Key_R);
    rightAlignAct->setCheckable(true);
    rightAlignAct->setToolTip("右对齐");
    rightAlignAct->setStatusTip(tr("将选择的文字进行右对齐"));

    justifyAct->setShortcut(Qt::CTRL + Qt::Key_J);
    justifyAct->setCheckable(true);
    justifyAct->setToolTip("两端对齐");
    justifyAct->setStatusTip(tr("将选择的文字进行两端对齐"));

    QPixmap pix(16,16);
    pix.fill(Qt::red);
    colorAct = new QAction(pix,tr("颜色(&C)..."),this);
    colorAct->setToolTip("颜色");
    colorAct->setStatusTip(tr("设置为选中的颜色"));
    connect(colorAct,&QAction::triggered,this,&MainWindow::textColor);

    //没有设置快捷键？？
    closeAct = new QAction(tr("关闭(&O)"),this);
    closeAct->setStatusTip(tr("关闭活动的word子窗口"));
    connect(closeAct,&QAction::triggered,mdiArea,&QMdiArea::closeActiveSubWindow);

    closeAllAct = new QAction(tr("关闭所有(&A)"),this);
    closeAllAct->setStatusTip(tr("关闭全部子窗口"));
    connect(closeAllAct,&QAction::triggered,mdiArea,&QMdiArea::closeAllSubWindows);

    tileAct = new QAction(tr("平铺(&T)"),this);
    tileAct->setStatusTip(tr("平铺子窗口"));
    connect(tileAct,&QAction::triggered,mdiArea,&QMdiArea::tileSubWindows);

    cascadeAct = new QAction(tr("层叠（&C"),this);
    cascadeAct->setStatusTip(tr("层叠子窗口"));
    connect(cascadeAct,&QAction::triggered,mdiArea,&QMdiArea::cascadeSubWindows);

    nextAct = new QAction(tr("下一个(&T)"),this);
    nextAct->setShortcuts(QKeySequence::NextChild);
    nextAct->setStatusTip(tr("移动焦点到下一个子窗口"));
    connect(nextAct,&QAction::triggered,mdiArea,&QMdiArea::activateNextSubWindow);

    previousAct = new QAction(tr("前一个(&V)"),this);
    previousAct->setShortcuts(QKeySequence::PreviousChild);
    previousAct->setStatusTip(tr("移动焦点到上一个子窗口"));
    connect(previousAct,&QAction::triggered,mdiArea,&QMdiArea::activatePreviousSubWindow);

    separatorAct = new QAction(this);
    separatorAct->setSeparator(true);

    aboutAct = new QAction(tr("关于(&A)"),this);
    aboutAct->setStatusTip("关于此软件的相关信息");
    connect(aboutAct,&QAction::triggered,this,&MainWindow::about);

    aboutQtAct = new QAction(tr("关于Qt(&Q)"),this);
    aboutQtAct->setStatusTip("关于Qt库相关信息");
    connect(aboutQtAct,&QAction::triggered,this,&MainWindow::aboutQt);
}
void MainWindow::creatMenus()      //创建菜单
{
    fileMenu = menuBar()->addMenu(tr("文件(&F)"));
    fileMenu->addAction(newAct);
    fileMenu->addAction(openAct);
    fileMenu->addSeparator();//分隔符
    fileMenu->addAction(saveAct);
    fileMenu->addAction(saveAsAct);
    fileMenu->addSeparator();
    fileMenu->addAction(printAct);
    fileMenu->addAction(printPreviewAct);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAct);

    editMenu = menuBar()->addMenu(tr("编辑(&E)"));
    editMenu->addAction(undoAct);
    editMenu->addAction(redoAct);
    editMenu->addSeparator();
    editMenu->addAction(cutAct);
    editMenu->addAction(copyAct);
    editMenu->addAction(pasteAct);

    formatMenu = menuBar()->addMenu(tr("格式(&O)"));
    fontMenu = formatMenu->addMenu(tr("字体(&D)"));
    fontMenu->addAction(boldAct);
    fontMenu->addAction(italicAct);
    fontMenu->addAction(underlineAct);
    alignMenu = formatMenu->addMenu(tr("段落"));
    alignMenu->addAction(leftAlignAct);
    alignMenu->addAction(centerAct);
    alignMenu->addAction(rightAlignAct);
    alignMenu->addAction(justifyAct);
    formatMenu->addAction(colorAct);
    connect(formatMenu,&QMenu::aboutToShow,this,&MainWindow::enabledText);//设置段落格式粗体斜体下划线和颜色

    windowMenu = menuBar()->addMenu(tr("窗口(&W)"));
    updateWindowMenu();
    connect(windowMenu,&QMenu::aboutToShow,this,&MainWindow::updateWindowMenu);
    menuBar()->addSeparator();

    helpMenu = menuBar()->addMenu(tr("帮助(&H)"));
    helpMenu->addAction(aboutAct);
    helpMenu->addSeparator();
    helpMenu->addAction(aboutQtAct);
}
void MainWindow::creatToolBars()   //创建工具条
{
    fileToolBar = addToolBar(tr("文件"));
    fileToolBar->addAction(newAct);
    fileToolBar->addAction(openAct);
    fileToolBar->addAction(saveAct);
    fileToolBar->addSeparator();
    fileToolBar->addAction(printAct);

    editToolBar = addToolBar(tr("编辑"));
    editToolBar->addAction(undoAct);
    editToolBar->addAction(redoAct);
    editToolBar->addSeparator();
    editToolBar->addAction(cutAct);
    editToolBar->addAction(copyAct);
    editToolBar->addAction(pasteAct);

    editToolBar = addToolBar(tr("格式"));
    editToolBar->addAction(boldAct);
    editToolBar->addAction(italicAct);
    editToolBar->addAction(underlineAct);
    editToolBar->addSeparator();
    editToolBar->addAction(leftAlignAct);
    editToolBar->addAction(centerAct);
    editToolBar->addAction(rightAlignAct);
    editToolBar->addAction(justifyAct);
    editToolBar->addSeparator();
    editToolBar->addAction(colorAct);

    addToolBarBreak(Qt::TopToolBarArea);
    comboToolBar = addToolBar(tr("组合选择"));
    comboStyle = new QComboBox();
    comboToolBar->addWidget(comboStyle);
    comboStyle->addItem("标准");
    comboStyle->addItem("项目符号(●)");
    comboStyle->addItem("项目符号(○)");
    comboStyle->addItem("项目符号(■)");
    comboStyle->addItem("编号(⒈⒉⒊)");
    comboStyle->addItem("编号( a.b.c.)");
    comboStyle->addItem("编号(A.B.C.)");
    comboStyle->addItem("编号(ⅰ.ⅱ.ⅲ.)");
    comboStyle->addItem("编号(Ⅰ.Ⅱ.Ⅲ.)");
    comboStyle->setStatusTip("段落加上编号或标号");
    connect(comboStyle,&QComboBox::activated,this,&MainWindow::textStyle);

    comboFont = new QFontComboBox();
    comboToolBar->addWidget(comboFont);
    comboFont->setStatusTip("更改字体");
    //connect(comboFont,&QFontComboBox::activated,this,&MainWindow::textFamily);
    connect(comboFont,&QFontComboBox::currentFontChanged,this,
            [=](const QFont &f){
                textFamily(f);
            }

            );

    comboSize = new QComboBox();
    comboToolBar->addWidget(comboSize);
    comboSize->setEditable(true);
    comboSize->setStatusTip("更改字号");

    QList<int> list;
    list<<6<<7<<8<<9<<10<<11<<12<<14<<16<<18<<20<<22<<24<<26<<28<<36<<48<<72;
    for(int size:list)
        comboSize->addItem(QString::number(size));
    connect(comboSize,&QComboBox::currentIndexChanged, this,&MainWindow::textSize);
    comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font().pointSize())));
}
void MainWindow::creatStatusBar()  //创建状态条
{
    statusBar()->showMessage(tr("就绪"));
}
void MainWindow::enabledText()//设置段落格式粗体斜体下划线和颜色功能开启
{
    boldAct->setEnabled(true);
    italicAct->setEnabled(true);
    underlineAct->setEnabled(true);
    leftAlignAct->setEnabled(true);
    centerAct->setEnabled(true);
    rightAlignAct->setEnabled(true);
    justifyAct->setEnabled(true);
    colorAct->setEnabled(true);
}
void MainWindow::fontChanged(const QFont &f)//选择字体样式，粗体斜体下划线
{
    comboFont->setCurrentIndex(comboFont->findText(static_cast<QString>(QFontInfo(f).family())));
    comboSize->setCurrentIndex(comboSize->findText(QString::number((f.pointSize()))));

    boldAct->setChecked(f.bold());
    italicAct->setChecked(f.italic());
    underlineAct->setChecked(f.underline());
}
void MainWindow::colorChanged(const QColor &c)//选择颜色并将选择的颜色设置到图标
{
    QPixmap pix(16,16);
    pix.fill(c);
    colorAct->setIcon(pix);
}
void MainWindow::alignmentChanged(Qt::Alignment a)//设置段落格式
{
    if(a & Qt::AlignLeft)
    {
        leftAlignAct->setChecked(true);
    }
    else if(a & Qt::AlignCenter)
    {
        centerAct->setChecked(true);
    }
    else if(a & Qt::AlignRight)
    {
        rightAlignAct->setChecked(true);
    }
    else if(a & Qt::AlignJustify)
    {
        justifyAct->setChecked(true);
    }
}
child *MainWindow::creatchild()
{
    child * newchild = new child;

    mdiArea->addSubWindow(newchild);
    connect(newchild,&child::copyAvailable,cutAct,&QAction::setEnabled);
    connect(newchild,&child::copyAvailable,copyAct,&QAction::setEnabled);
    return newchild;
}
child *MainWindow::activeChils()//激活子窗口
{
    if(QMdiSubWindow * activeSubWindow = mdiArea->activeSubWindow())
    {
        return qobject_cast<child *>(activeSubWindow->widget());
    }
    return 0;
}
QMdiSubWindow * MainWindow::findChild(const QString &filename)//文件使用
{
    QString filepath = QFileInfo(filename).canonicalFilePath();
    //获取到路径
    for(QMdiSubWindow * window : mdiArea->subWindowList())
    {
        child *mychild = qobject_cast<child *>(window->widget());
        if(mychild->currentFile() == filepath)
        {
            return window;
        }
    }
    return 0;
}

